home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 3006 / 3006.xpi / chrome / dwhelper.jar / content / dwhelper.js < prev    next >
Text File  |  2010-01-15  |  10KB  |  302 lines

  1. /******************************************************************************
  2.  *            Copyright (c) 2006-2009 Michel Gutierrez. All Rights Reserved.
  3.  ******************************************************************************/
  4.  
  5. var DWHelper={
  6.     uuid: "{b9db16a4-6edc-47ec-a1f4-b86292ed211d}",
  7.     core: Components.classes["@downloadhelper.net/core;1"].getService(Components.interfaces.dhICore),
  8.     prefViewers: [
  9.                   ["dwhelper-statusbarpanel","show-in-statusbar"],
  10.                   ["dwhelper-toolsmenu","show-in-toolsmenu"],
  11.                   ["dwhelper-toolsmenu-separator","show-in-toolsmenu"],
  12.                   ["dwhelper-ctxmenu","context-menu"],
  13.     ],
  14.     quickKeys: [
  15.                 "quickkey",
  16.                 "opendirkey"
  17.     ],
  18.     startupServices: [
  19.                       "@downloadhelper.net/license-handler;1",
  20.                       "@downloadhelper.net/convconf-handler;1",
  21.                       "@downloadhelper.net/safe-mode-handler;1",
  22.                       "@downloadhelper.net/youtube-probe;1",
  23.                       "@downloadhelper.net/medialink-probe;1",
  24.                       "@downloadhelper.net/network-probe;1",
  25.                       "@downloadhelper.net/download-processor;1",
  26.                       "@downloadhelper.net/quick-download-processor;1",
  27.                       "@downloadhelper.net/download-convert-processor;1",
  28.                       "@downloadhelper.net/secrethelper-intro-processor;1",
  29.                       "@downloadhelper.net/twitter-processor;1",
  30.                       "@downloadhelper.net/flashgot-download-processor;1",
  31.                       "@downloadhelper.net/copyurl-processor;1",
  32.                       "@downloadhelper.net/dump-processor;1",
  33.                       "@downloadhelper.net/add-to-blacklist-processor;1",
  34.                       "@downloadhelper.net/mp3tunes-mobile-processor;1",
  35.                       "@downloadhelper.net/mp3tunes-locker-processor;1",
  36.     ],
  37.     pref: Components.classes["@mozilla.org/preferences-service;1"]
  38.                             .getService(Components.interfaces.nsIPrefService).getBranch("dwhelper."),
  39.     util: Components.classes["@downloadhelper.net/util-service;1"]
  40.                              .getService(Components.interfaces.dhIUtilService),
  41.     firstSessionLaunch: true,
  42. };
  43.  
  44. DWHelper.version=DWHelper.util.getVersion(DWHelper.uuid);
  45.  
  46. window.addEventListener("load", DWHelper_onLoad, false);
  47. window.addEventListener("unload", DWHelper_onUnload, false);
  48.  
  49. for(var i in DWHelper.startupServices) {
  50.     var service=DWHelper.startupServices[i];
  51.     try {
  52.         Components.classes[service].getService();
  53.         //dump("[DWHelper/startup]: loaded service "+service+"\n");
  54.     } catch(e) {
  55.         dump("!!! [DWHelper/startup]: loading service "+service+": "+e+"\n");
  56.     }
  57. }
  58.  
  59. function DWHelper_onLoad(e) {
  60.  
  61.     try {
  62.         if(DWHelper.firstSessionLaunch) {
  63.             DWHelper_fixPreferences();
  64.             DWHelper_updateKeys();
  65.             DWHelper.firstSessionLaunch=false;
  66.             var version=DWHelper.version;
  67.             if(DWHelper.pref.getBoolPref("first-time")) {
  68.                 try {
  69.                     function welcome(version) {
  70.                         try {
  71.                             var browser = top.getBrowser();
  72.                             browser.selectedTab=browser.addTab("http://www.downloadhelper.net/welcome.php?version="+version);
  73.                         } catch(e) {
  74.                         }
  75.                     }
  76.                     if(DWHelper.pref.getBoolPref("do-welcome"))
  77.                         setTimeout(welcome,100,version);
  78.                 } catch(e) {
  79.                 }
  80.                 DWHelper.pref.setBoolPref("first-time",false);
  81.                 DWHelper.pref.setCharPref("last-version",version);
  82.                 DWHelper_install();
  83.             } else {
  84.                 var lastVersion="1.95";
  85.                 try {
  86.                     lastVersion=DWHelper.pref.getCharPref("last-version");
  87.                 } catch(e) {}
  88.                 if(lastVersion!=version) {
  89.                     DWHelper.pref.setCharPref("last-version",version);
  90.                     try {
  91.                         function install(lastVersion,version,cwe) {
  92.                             try {
  93.                                 var browser = top.getBrowser();
  94.                                 if(cwe)
  95.                                     browser.selectedTab=browser.addTab("http://www.downloadhelper.net/update.php?to="+version+"&from="+lastVersion);
  96.                                 else
  97.                                     browser.selectedTab=browser.addTab("http://www.downloadhelper.net/update.php?from="+lastVersion+"&to="+version);
  98.                             } catch(e) {
  99.                             }
  100.                         }
  101.                         var cwe=false;
  102.                         try {
  103.                             cwe=DWHelper.pref.getBoolPref("conversion-was-enabled");
  104.                         } catch(e) {}
  105.                         setTimeout(install,100,lastVersion,version,cwe);
  106.                     } catch(e) {}
  107.                 }
  108.             }
  109.             for(var i in DWHelper.prefViewers) {
  110.                 DWHelper_updateWidgetVisibility(DWHelper.prefViewers[i][0],DWHelper.prefViewers[i][1]);
  111.             }
  112.             DWHelper.core.registerWindow(window);
  113.         }
  114.     } catch(e) {
  115.         dump("!!! [DWHelper] onload(): "+e+"\n");
  116.     }
  117. }
  118.  
  119. function DWHelper_onUnload(e) {
  120.     DWHelper.core.unregisterWindow(window);
  121. }
  122.  
  123. function DWHelper_fixPreferences() {
  124.     var downloadMode="controlled";
  125.     try {
  126.         downloadMode=DWHelper.pref.getCharPref("download-mode");
  127.     } catch(e) {}
  128.     if(downloadMode=="flashgot") {
  129.         try {
  130.             Components.classes["@maone.net/flashgot-service;1"].
  131.                 getService(Components.interfaces.nsISupports).wrappedJSObject;
  132.             DWHelper.pref.setCharPref("processor-keymap","0:flashgot-download,2:convert-choice,3:quick-download");
  133.         } catch(e) {}
  134.         DWHelper.pref.setCharPref("download-mode","controlled");
  135.     }
  136.     
  137.     function fixPrefKey(prefId) {
  138.         try {
  139.             DWHelper.pref.getCharPref(prefId);
  140.         } catch(e) {
  141.             var key=DWHelper.pref.getCharPref(prefId+".key");
  142.             var modifier=DWHelper.pref.getCharPref(prefId+".modifier");
  143.             var modifiers=modifier.split(" ");
  144.             modifier=0;
  145.             for(var i=0;i<modifiers.length;i++) {
  146.                 if(modifiers[i]=="control")
  147.                     modifier|=1;
  148.                 if(modifiers[i]=="shift")
  149.                     modifier|=2;
  150.                 if(modifiers[i]=="alt")
  151.                     modifier|=4;
  152.             }
  153.             DWHelper.pref.setCharPref(prefId,modifier+";"+key);
  154.         }
  155.     }
  156.     fixPrefKey("quickkey");
  157.     fixPrefKey("opendirkey");
  158.     
  159.     try {
  160.         DWHelper.pref.getIntPref("menu-expiration");
  161.     } catch(e) {
  162.         var expiration=60;
  163.         try {
  164.             expiration=parseInt(DWHelper.pref.getIntPref("menu-http-expiration"))/1000;
  165.             expiration=parseInt(""+expiration);
  166.         } catch(e) {}
  167.         DWHelper.pref.setIntPref("menu-expiration",expiration);
  168.     }
  169.  
  170.     try {
  171.         DWHelper.pref.getIntPref("mediaweight");
  172.     } catch(e) {
  173.         var mediaweightEnabled=DWHelper.pref.getBoolPref("mediaweight-enabled");
  174.         var mediaweightThreshold=DWHelper.pref.getIntPref("mediaweight-threshold");
  175.         var mediaweight="";
  176.         if(mediaweightEnabled) {
  177.             mediaweight=""+parseInt(""+(mediaweightThreshold/1024));
  178.         }
  179.         DWHelper.pref.setCharPref("mediaweight",mediaweight);
  180.     }
  181.     
  182.     var encoders=["ffmpeg","mencoder"];
  183.     for(var i in encoders) {
  184.         var encoder=encoders[i];
  185.         try {
  186.             DWHelper.pref.getCharPref("converter-path-"+encoder);
  187.         } catch(e) {
  188.             try {
  189.                 var encoderPath=DWHelper.pref.getCharPref(encoder+"-path");
  190.                 DWHelper.pref.setCharPref("converter-path-"+encoder,encoderPath);
  191.             } catch(e) {}
  192.         }
  193.     }
  194. }
  195.  
  196. function DWHelper_install() {
  197.     try {
  198.         var dwhelperId="dwhelper-toolbaritem";
  199.         var afterId="urlbar-container";
  200.         var afterElem=document.getElementById(afterId);
  201.         if(afterElem) {
  202.             var navBar=afterElem.parentNode;
  203.             if(document.getElementById(dwhelperId)==null) {
  204.                 // waiting for firefox bug 403959 to be solved
  205.                 //navBar.insertItem(dwhelperId,afterElem.nextSibling);
  206.                 navBar.insertItem(dwhelperId,afterElem);
  207.                 navBar.setAttribute("currentset", navBar.currentSet );
  208.                 document.persist("nav-bar", "currentset");
  209.             }
  210.         }        
  211.     } catch(e) {
  212.     }
  213. }
  214.  
  215. function DWHelper_observer() {
  216. }
  217. DWHelper_observer.prototype={
  218.     observe: function(subject, topic , data) {
  219.         if(topic=="nsPref:changed") {
  220.             for(var i in DWHelper.prefViewers) {
  221.                 if(DWHelper.prefViewers[i][1]==data) {
  222.                     DWHelper_updateWidgetVisibility(DWHelper.prefViewers[i][0],DWHelper.prefViewers[i][1]);
  223.                 }
  224.             }
  225.             for(var i in DWHelper.quickKeys) {
  226.                 var key="dwhelper."+DWHelper.quickKeys[i];
  227.                 if(data.substr(0,key.length)==key) {
  228.                     DWHelper_updateKeys();
  229.                     break;
  230.                 }
  231.             }
  232.         }
  233.     },
  234.     QueryInterface: function(iid) {
  235.         if(
  236.             iid.equals(Components.interfaces.nsIObserver)==false &&
  237.             iid.equals(Components.interfaces.nsISupports)==false
  238.         ) {
  239.                 throw Components.results.NS_ERROR_NO_INTERFACE;
  240.             }
  241.         return this;
  242.     }
  243.     
  244. }
  245.  
  246. DWHelper.observer=new DWHelper_observer();
  247. DWHelper.prefBranch2=Components.classes["@mozilla.org/preferences-service;1"]
  248.                                         .getService(Components.interfaces.nsIPrefService)
  249.                                         .getBranch("dwhelper.")
  250.                                         .QueryInterface(Components.interfaces.nsIPrefBranch2);
  251. DWHelper.prefBranch2.addObserver("", DWHelper.observer, false);
  252.  
  253. function DWHelper_updateWidgetVisibility(widgetId,prefId) {
  254.     //dump("DWHelper_updateWidgetVisibility("+widgetId+","+prefId+")\n");
  255.     var widget=document.getElementById(widgetId);
  256.     if(widget) {
  257.         var enabled=false;
  258.         try {
  259.             enabled=DWHelper.pref.getBoolPref(prefId);
  260.         } catch(e) {}
  261.         //dump("=>"+enabled+"\n");
  262.         widget.setAttribute("hidden",""+!enabled);
  263.     } 
  264. }
  265.  
  266. function DWHelper_updateKeys() {
  267.     function updateKey(defKey,keyElemId,prefName) {
  268.         var key=defKey;
  269.         try {
  270.             key=DWHelper.pref.getCharPref(prefName);
  271.         } catch(e) {}
  272.         var keyParts=key.split(";",2);
  273.         var key=keyParts[1];
  274.         var modifier=keyParts[0];
  275.         if(!/^VK_/.test(key))
  276.             key="VK_"+key;
  277.     
  278.         var keyElem=document.getElementById(keyElemId);
  279.         if(keyElem) {
  280.             if(/^VK_.$/.test(key)) {
  281.                 var m=/^VK_(.)$/.exec(key);
  282.                 keyElem.removeAttribute("keycode");
  283.                 keyElem.setAttribute("key",m[1]);
  284.             } else {
  285.                 keyElem.removeAttribute("key");
  286.                 keyElem.setAttribute("keycode",key);
  287.             }
  288.             var modifiers=[];
  289.             if(modifier&1)
  290.                 modifiers.push("control");
  291.             if(modifier&2)
  292.                 modifiers.push("shift");
  293.             if(modifier&4)
  294.                 modifiers.push("alt");
  295.             keyElem.setAttribute("modifiers",modifiers.join(" "));
  296.         }
  297.     }
  298.     updateKey("3;VK_Q","dwhelper-quick-key","quickkey");
  299.     updateKey("3;VK_O","dwhelper-opendir-key","opendirkey");
  300. }
  301.  
  302.